package yxy.game.pm2.command.temple;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.Hero;
import yxy.game.pm2.bean.currency.Currencies;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.TempleMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.hero.HeroHandle;

import java.util.ArrayList;

/** 置换神殿 高级祭坛选择武将 */
@MessageType(proto = Command.SeniorAltarSelectHero, classtype = TempleMessage.SeniorAltarSelectHeroRequest.class)
public class CMD_SeniorAltarSelectHero extends Command<TempleMessage.SeniorAltarSelectHeroRequest> {

	@Override
	public void execute(MessageSource messageSource, TempleMessage.SeniorAltarSelectHeroRequest request) {
		String playerCode = messageSource.getTag();
		String heroCode = request.getHero();

		TempleMessage.SeniorAltarSelectHeroResponse.Builder response = TempleMessage.SeniorAltarSelectHeroResponse.newBuilder();

		// TODO 处理响应封装
		Hero hero = new HeroHandle().getHero(playerCode, heroCode);
		if (hero == null) {
			send(messageSource, response, Result.HeroNotExists);
			return;// 武将不存在
		}

		Currencies back = new Currencies();
		ArrayList<Currency> backHero = new ArrayList<>();
		Result result = new HeroHandle().seniorAltarBackCurrency(playerCode, hero, back, backHero);
		response.addAllBack(new CurrencyHandle().packItems(backHero));
		response.addAllBack(new CurrencyHandle().packItems(back));

		send(messageSource, response, result);
	}

	private void send(MessageSource messageSource, TempleMessage.SeniorAltarSelectHeroResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}